/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.dao.mock;

import java.util.LinkedList;
import java.util.List;

import org.activequant.core.domainmodel.InstrumentSpecification;
import org.activequant.core.domainmodel.SeriesSpecification;
import org.activequant.core.domainmodel.data.MarketDataEntity;
import org.activequant.dao.IMarketDataEntityDao;
import org.activequant.util.exceptions.DaoException;
import org.apache.log4j.Logger;

/**
 * Base class for mock Dao implementations. Holds objects in memory, using TreeMap.
 * <br>
 * <b>History:</b><br>
 *  - [Oct 9, 2007] Created (Mike Kroutikov)<br>
 *  - [27.10.2007] Switched to IMarketDataEntity, refactoring MockDaoSupportBase<T> (Erik Nijkamp)<br>
 *  - [27.10.2007] Replaced abstract methods (compareByQuery, ...) with protected default implementations (Erik Nijkamp)<br>
 *
 *  @author Mike Kroutikov
 */
abstract class MockMarketDataEntityBase<T extends MarketDataEntity<T>> extends MockDaoSupportBase<T> implements IMarketDataEntityDao<T> {

	final Logger log = Logger.getLogger(getClass());
	
	protected MockMarketDataEntityBase(Class<T> clazz) {
		super(clazz);
	}
	
	Long getId(T t) {
		return t.getId();
	}
	
	void setId(T t, Long id) {
		t.setId(id);
	}
	
	protected boolean compareByQuery(SeriesSpecification query, T sample) {
		
		if(query.getInstrumentSpecification() != null) {
			if(query.getInstrumentSpecification().equals(sample.getInstrumentSpecification()) == false) {
				return false;
			}
		}
		
		if(query.getStartTimeStamp() != null) {
			if(query.getStartTimeStamp().compareTo(sample.getTimeStamp()) > 0) {
				return false;
			}
		}

		if(query.getEndTimeStamp() != null) {
			if(query.getEndTimeStamp().compareTo(sample.getTimeStamp()) < 0) {
				return false;
			}
		}
		
		return true;
	}
	
	protected boolean compareByContract(InstrumentSpecification spec, T sample) {
		return(spec.equals(sample.getInstrumentSpecification()));
	}

	public T[] findByInstrumentSpecification(InstrumentSpecification spec)
			throws DaoException {
		List<T> out = new LinkedList<T>();
		for(T t : database.values()) {
			if(compareByContract(spec, t)) {
				out.add(t);
			}
		}
		
		return out.toArray(createArray(0));
	}

	public T[] findBySeriesSpecification(SeriesSpecification contractQuery)
			throws DaoException {
		List<T> out = new LinkedList<T>();
		for(T t : database.values()) {
			if(compareByQuery(contractQuery, t)) {
				out.add(t);
			}
		}
		
		return out.toArray(createArray(0));
	}

	public void deleteByInstrumentSpecification(InstrumentSpecification spec)
			throws DaoException {
		T[] toDelete = findByInstrumentSpecification(spec);
		
		for(T t: toDelete) {
			delete(t);
		}
	}

	public void deleteBySeriesSpecification(SeriesSpecification contractQuery)
			throws DaoException {
		T[] toDelete = findBySeriesSpecification(contractQuery);
		
		for(T t: toDelete) {
			delete(t);
		}
	}
}
